package com.tchintech.pay.view.certification;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;

import com.google.gson.Gson;
import com.tchintech.pay.R;
import com.tchintech.pay.delegate.certification.BankCardInfoDelegate;
import com.tchintech.pay.frame.presenter.ActivityPresenter;
import com.tchintech.pay.model.entity.AuthenticateResult;
import com.tchintech.pay.model.entity.BankCodeInfo;
import com.tchintech.pay.model.entity.BankInfoResult;
import com.tchintech.pay.model.entity.BankResult;
import com.tchintech.pay.model.entity.BankTypeInfo;
import com.tchintech.pay.model.entity.CityCodeInfo;
import com.tchintech.pay.model.entity.CityResult;
import com.tchintech.pay.model.entity.HttpCode;
import com.tchintech.pay.model.entity.ResultBean;
import com.tchintech.pay.model.entity.UserBankInfo;
import com.tchintech.pay.net.HttpRequester;
import com.tchintech.pay.utils.AppConst;
import com.tchintech.pay.utils.RegexUtils;

import java.io.File;
import java.util.HashMap;
import java.util.Map;

/**
 * Created by Administrator on 2017/7/7 0007.
 */

public class BankCardInfoActivity extends ActivityPresenter<BankCardInfoDelegate> implements View.OnClickListener{
    private String URL_Province = "Bank/accountProvince";
    private String URL_City = "Bank/accountCity";
    private String URL_BankType = "Bank/bankType";
    private String URL_BankCode = "Bank/bankCode";
    private String URL_AddBankCardInfo = "Merchant/addBankCardInfo";
    private ArrayAdapter<CityCodeInfo> arr_adapter_city;
    private ArrayAdapter<BankTypeInfo> arr_adapter_bank;
    private ArrayAdapter<BankCodeInfo> arr_adapter_bank_type;
    private Map<String,String> map;
    private UserBankInfo userBankInfo = null;
    private Handler handler;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    protected void initView() {
        super.initView();
        handler();
        viewDelegate.setOnClickListener(this,R.id.bankCardInfo_bt_confirm, R.id.bankCardInfo_bt_cancel);
    }

    @Override
    protected void initData() {
        super.initData();
        map = new HashMap<>();
        httpPost(URL_Province,map, HttpCode.bankcard,null);
    }


    @Override
    public void httpCallback(String resultBean, int what) {
        super.httpCallback(resultBean, what);
        Message msg=new Message();

        switch (what){
            /*case  HttpCode.bankcard:
                if(resultBean.getResCode().equals("1")){
                    msg.what=1;
                    msg.obj=resultBean.getResult();
                    //成功
                }else{
                    msg.what=2;
                    msg.obj=resultBean.getResult();
                    //失败
                }
                break;
            case HttpCode.bankaccount:
                //省市
                if(resultBean.getResCode().equals("1")){
                    msg.what=3;
                    msg.obj=resultBean.getResult();
                    //成功
                }else{
                    msg.what=4;
                    msg.obj=resultBean.getResult();
                    //失败
                }
                break;
            case HttpCode.banktype:
                //银行种类
                if(resultBean.getResCode().equals("1")){
                    msg.what=5;
                    msg.obj=resultBean.getResult();
                    //成功
                }else{
                    msg.what=6;
                    msg.obj=resultBean.getResult();
                    //失败
                }
                break;
            case HttpCode.bankCode:
                //开户行名称
                if(resultBean.getResCode().equals("1")){
                    msg.what=7;
                    msg.obj=resultBean.getResult();
                    //成功
                }else{
                    msg.what=8;
                    msg.obj=resultBean.getResult();
                    //失败
                }
                break;
            case HttpCode.banksubmit:
                //最后确认提交
                if(resultBean.getResCode().equals("1")){
                    msg.what=9;
                    msg.obj=resultBean.getResult();
                    //成功
                }else{
                    msg.what=10;
                    msg.obj=resultBean.getResult();
                    //失败
                }
                break;*/
        }
        handler.sendMessage(msg);
    }


    private void handler(){
        handler=new Handler(){
            @Override
            public void handleMessage(Message msg) {
                super.handleMessage(msg);
                Gson gson=new Gson();
                String messgae= (String) msg.obj;
                switch (msg.what){
                    case 1:
                        userBankInfo = new UserBankInfo();
                        CityResult provinceResult = gson.fromJson(messgae,CityResult.class);
                        arr_adapter_city = new ArrayAdapter<>(
                                BankCardInfoActivity.this,
                                android.R.layout.simple_spinner_item,
                                provinceResult.getLsit());
                        // 设置样式
                        arr_adapter_city.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
                        // 加载适配器
                        viewDelegate.getSpinner(R.id.bankCardInfo_sp_accountProvince).setAdapter(arr_adapter_city);
                        viewDelegate.getSpinner(R.id.bankCardInfo_sp_accountProvince)
                                .setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
                                    @Override
                                    public void onItemSelected(AdapterView<?> parent,
                                                               View view, int position, long id) {
                                        CityCodeInfo cityCodeInfo = (CityCodeInfo)   viewDelegate.getSpinner(R.id.bankCardInfo_sp_accountProvince)
                                                .getItemAtPosition(position);
                                        userBankInfo.setAccountProvince(cityCodeInfo.getCityCode());
                                        map = new HashMap<>();
                                        map.put("accountProvinceCode",
                                                cityCodeInfo.getCityCode());
                                        try {
                                         //   LogUtils.e(URL_City);
                                            httpPost(URL_City, map, HttpCode.bankaccount, null);
                                            // 更新市级地区
                                        } catch (Exception e) {
                                            e.printStackTrace();
                                        }
                                    }

                                    @Override
                                    public void onNothingSelected(AdapterView<?> parent) {
                                    }
                                });

                        break;
                    case 2:
                        break;
                    case 3:
                        CityResult cityResult = gson.fromJson(messgae,CityResult.class);
                        arr_adapter_city = new ArrayAdapter<>(
                                BankCardInfoActivity.this,
                                android.R.layout.simple_spinner_item,
                                cityResult.getLsit());
                        // 设置样式
                        arr_adapter_city
                                .setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);

                        // 加载适配器
                        viewDelegate.getSpinner(R.id.bankCardInfo_sp_accountCity).setAdapter(arr_adapter_city);
                        viewDelegate.getSpinner(R.id.bankCardInfo_sp_accountCity)
                                .setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
                                    @Override
                                    public void onItemSelected(AdapterView<?> parent,
                                                               View view, int position, long id) {
                                        CityCodeInfo cityCodeInfo = (CityCodeInfo) viewDelegate.getSpinner(R.id.bankCardInfo_sp_accountCity)
                                                .getItemAtPosition(position);
                                        userBankInfo.setCityCode(cityCodeInfo.getCityCode());
                                        map = new HashMap<>();
                                        try {
                                            httpPost(URL_BankType,map,HttpCode.banktype,null);
                                        } catch (Exception e) {
                                            // TODO Auto-generated catch block
                                            e.printStackTrace();
                                        }
                                    }

                                    @Override
                                    public void onNothingSelected(AdapterView<?> parent) {

                                    }
                                });

                        break;
                    case 4:
                        break;
                    case 5:
                        final BankResult bankResult = (BankResult) msg.obj;
                        arr_adapter_bank = new ArrayAdapter<>(
                                BankCardInfoActivity.this,
                                android.R.layout.simple_spinner_item,
                                bankResult.getLsit());
                        // 设置样式
                        arr_adapter_bank
                                .setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
                        viewDelegate.getSpinner(R.id.bankCardInfo_sp_bankType).setAdapter(arr_adapter_bank);
                        viewDelegate.getSpinner(R.id.bankCardInfo_sp_bankType)
                                .setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {

                                    @Override
                                    public void onItemSelected(AdapterView<?> parent,
                                                               View view, int position, long id) {
                                        BankTypeInfo bankTypeInfo = (BankTypeInfo) viewDelegate.getSpinner(R.id.bankCardInfo_sp_bankType)
                                                .getItemAtPosition(position);
                                        userBankInfo.setBankType(bankTypeInfo.getBankType());
//                                    userBankInfo.setBankAccount(bankTypeInfo.);
                                        map = new HashMap<>();
                                        map.put("bankType", userBankInfo.getBankType());
                                        map.put("cityCode", userBankInfo.getCityCode());
                                        // bankAccount = b
                                        try {
                                            httpPost(URL_BankCode,map,HttpCode.bankCode,null);
                                        } catch (Exception e) {
                                            // TODO Auto-generated catch block
                                            e.printStackTrace();
                                        }

                                    }

                                    @Override
                                    public void onNothingSelected(AdapterView<?> parent) {

                                    }
                                });

                        break;
                    case 6:
                        break;
                    case 7:
                        BankInfoResult bankInfoResult = (BankInfoResult) msg.obj;
                        // // 适配器
                        arr_adapter_bank_type = new ArrayAdapter<>(
                                BankCardInfoActivity.this,
                                android.R.layout.simple_spinner_item,
                                bankInfoResult.getLsit());
                        // 设置样式
                        arr_adapter_city
                                .setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);

                        // 加载适配器
                        viewDelegate.getSpinner(R.id.bankCardInfo_sp_bankName).setAdapter(arr_adapter_bank_type);
                        viewDelegate.getSpinner(R.id.bankCardInfo_sp_bankName)
                                .setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
                                    @Override
                                    public void onItemSelected(AdapterView<?> parent,
                                                               View view, int position, long id) {
                                        BankCodeInfo bankCodeInfo = (BankCodeInfo)    viewDelegate.getSpinner(R.id.bankCardInfo_sp_bankName)
                                                .getItemAtPosition(position);
                                        userBankInfo.setBankAccount(bankCodeInfo.getBankType());
                                        userBankInfo.setAccountName(bankCodeInfo.getBankCode());
                                        userBankInfo.setAccountCity(bankCodeInfo.getCityCode());
                                    }

                                    @Override
                                    public void onNothingSelected(AdapterView<?> parent) {
                                    }
                                });


                        break;
                    case 8:
                        break;
                    case 9:
                        AuthenticateResult authenticateResult=gson.fromJson(messgae,AuthenticateResult.class);
                        //ShoperInfoFragment
                        break;
                    case 10:
                        break;
                }
            }
        };
    }


    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.bankCardInfo_bt_confirm:
                if (userBankInfo != null) {
                    //TODO 检测 数据
//                    if (userBankInfo.getBankAccount() != null && userBankInfo.getAccountCity() != null && userBankInfo.getAccountProvince() != null && userBankInfo.getAccountName() != null) {
                    String str_bankCardInfoTvBankCard =viewDelegate.getTexts();
                    if (RegexUtils.checkBankCard(str_bankCardInfoTvBankCard)) {
                        map = new HashMap<>();
                        map.put("bankCardNumber", str_bankCardInfoTvBankCard);
                        map.put("bankAccount", userBankInfo.getBankAccount());
                        map.put("accountCity", userBankInfo.getAccountCity());
                        map.put("accountProvince", userBankInfo.getAccountProvince());
                        map.put("accountName", userBankInfo.getAccountName());
                        try {
                            httpPost(URL_AddBankCardInfo,map,HttpCode.banksubmit,null);
                        } catch (Exception e) {
                            e.printStackTrace();
                        }
                    } else {
                        //TODO 银行卡号不合格
                    }
                       //TODO 信息不全
                } else {
                       //TODO 信息为空
                }
                break;
            case R.id.bankCardInfo_bt_cancel:
                    finish();
                break;
        }
    }
   /* //网路请求界面配置
    private void httpPost(String url, Map<String, String> map, int what, File file ) {
        String path = HttpRequester.path(url, map);
        try {
            showProgressDialog("", "加载中，请稍后...");
            HttpRequester.post(this, what, path, map, this, file);
        } catch (Exception e) {
            hideProgressDialog();
            showMsg("网络异常");
        }
    }*/
    @Override
    protected Class<BankCardInfoDelegate> getDelegateClass() {
        return BankCardInfoDelegate.class;
    }
}
